<?php

namespace App\Http\Controllers;

use App\Models\Goods;
use Illuminate\Contracts\View\View as ViewContracts;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class orderController
{
    public function session(Request $request)
    {
//        $request->session()->flash('status', 1);
        dump($request->session()->get('status'));
//        session(['a' => 'aaa', 'b' => 'bbb', 'c' => 'ccc']);
        dump($request->session()->has('a'));
        dump($request->session()->getId());
        dump(session('a'));
        dump(session()->all());
        dump($request->session()->all());
        session(['a' => 'aaa', 'b' => 'bbb', 'c' => 'ccc']);
        dump($request->session()->all());
        $request->session()->put(['a1' => 'aaa1', 'b1' => 'bbb1', 'c1' => 'ccc1']);
        dump($request->session()->pull('a1'));
        $request->session()->reflash();
    }
    public function index(Request $request, int $id = 0): void
    {
        dump($request->input());
        dump($request->old());

        dump('$request->expectsJson()：' . ($request->expectsJson() ? 'true' : 'false'));
        dump('$request->prefer()：', $request->prefers(['text/html', 'application/json', 'application/xml']));
        dump('$request->accepts(["text/html", "application/json"])：' . ($request->accepts([
                'text/html',
                'application/json',
            ]) ? 'true' : 'false'));
        dump('$request->getAcceptableContentTypes()：', $request->getAcceptableContentTypes());
        dump('$request->ips()：', $request->ips());
        dump('$request->ip()：' . $request->ip());
        dump('$request->bearerToken()：' . $request->bearerToken());
        dump('$request->hasHeader("X-Http-Header")：' . ($request->hasHeader('X-Http-Header') ? 'true' : 'false'));
        dump('$request->hasHeader("Accept")：' . ($request->hasHeader('Accept') ? 'true' : 'false'));
        dump('$request->hasHeader("Accept")：' . $request->header("Accept"));
        dump('$request->header("User-Agent")：' . $request->header("User-Agent"));
        dump('$request->header("Accept")：' . $request->header("Accept"));
        dump('$request->isMethod("POST")：' . ($request->isMethod('POST') ? 'true' : 'false'));
        dump('$request->isMethod("GET")：' . ($request->isMethod('GET') ? 'true' : 'false'));
        dump('$request->getMethod()：' . $request->getMethod());
        dump('$request->method()：' . $request->method());
        dump('$request->schemeAndHttpHost()：' . $request->schemeAndHttpHost());
        dump('$request->httpHost()：' . $request->httpHost());
        dump('$request->host()：' . $request->host());
        dump('$request->fullUrlWithoutQuery("aaa")：' . $request->fullUrlWithoutQuery('aaa'));
        dump('$request->fullUrlWithQuery(["type" => 1, "age" => 36])：' . $request->fullUrlWithQuery([
                "type" => 1,
                "age" => 36,
            ]));
        dump('$request->fullUrl()：' . $request->fullUrl());
        dump('$request->url()：' . $request->url());
        dump('$request->routeIs("admin.*")：' . ($request->routeIs('admin.*') ? 'true' : 'false'));
        dump('$request->routeIs("order.*")：' . ($request->routeIs('order.*') ? 'true' : 'false'));
        dump('$request->is("admin")：' . ($request->is('admin') ? 'true' : 'false'));
        dump('$request->is("order/*")：' . ($request->is('order/*') ? 'true' : 'false'));
        dump('$request->path()：' . $request->path());
        dump('Params：' . $id);
    }

    public function input(Request $request): Application|Redirector|RedirectResponse
    {
        $request->merge(['merge' => 'merge']);
        $request->merge(['aaa' => 'bbb']);
        $request->mergeIfMissing(['bbb' => 'aaa']);
        $request->mergeIfMissing(['bbb-ccc' => 'aaa']);
        dump($request->input());

        $request->whenFilled('name', function (string $input) {
            dump($input);
        }, function () {
            dump('None');
        });
        $request->whenFilled('name', function (string $input) {
            dump($input);
        });

        $request->whenHas('names', function (string $input) {
            dump($input);
        }, function () {
            dump('None');
        });
        $request->whenHas('aaa', function (string $input) {
            dump($input);
        });

        dump('$request->hasAny(["aaa", "time", "none"])', $request->hasAny(["aaa", "time", "none"]));
        dump('$request->hasAny(["aaa", "time"])', $request->hasAny(["aaa", "time"]));
        dump('$request->hasAny("aaa", "bbb", "none")', $request->hasAny("aaa", "time", "none"));
        dump('$request->hasAny("aaa", "bbb")', $request->hasAny("aaa", "time"));
        dump('$request->hasAny("date-no"])', $request->hasAny("date-no"));
        dump('$request->hasAny("date"])', $request->hasAny("date"));

        dump('$request->has(["aaa", "time", "none"])', $request->has(["aaa", "time", "none"]));
        dump('$request->has(["aaa", "time"])', $request->has(["aaa", "time"]));
        dump('$request->has("aaa", "bbb", "none")', $request->has("aaa", "time", "none"));
        dump('$request->has("aaa", "bbb")', $request->has("aaa", "time"));
        dump('$request->has("date-no"])', $request->has("date-no"));
        dump('$request->has("date"])', $request->has("date"));

        dump('$request->except(["aaa", "time", "none"])', $request->except(["aaa", "time", "none"]));
        dump('$request->except("aaa", "bbb")', $request->except("aaa", "time", "none"));
        dump('$request->except("date"])', $request->except("date"));

        dump('$request->only(["aaa", "time", "none"])', $request->only(["aaa", "time"]));
        dump('$request->only("aaa", "bbb")', $request->only("aaa", "time"));
        dump('$request->only("date"])', $request->only("date"));

        dump('$request->time：' . $request->time);
//        dump('$request->date("time", "!H:i")：' , $request->date("time", "!H:i", "Europe/Madrid"));
        dump('$request->date("date")：' . $request->date("date"));

        dump('$request->boolean("bool")：' . $request->boolean("bool"));
        dump('$request->boolean("bool")：', $request->boolean("bool"));
        dump('$request->string("str-trim")->trim()：', $request
            ->string("str-trim")
            ->trim());
        dump('$request->string("str")：', $request->string("str"));
        dump('$request->query("arr.a")：' . $request->query('arr.a', 'null'));
        dump('$request->query("asdf")：' . $request->query('asdf', 'asdf'));
        dump('$request->query("aaa")：' . $request->query('aaa'));
        dump('$request->query()：', $request->query());
        dump('$request->input("arr.a")：' . $request->input('arr.a', 'null'));
        dump('$request->input("asdf")：' . $request->input('asdf', 'asdf'));
        dump('$request->input("aaa")：' . $request->input('aaa'));
        dump('$request->input()：', $request->input());
        $request
            ->collect()
            ->each(function ($item, $key) {
                dump([$key => $item]);
            })
        ;
        dump('$request->collect()：', $request->collect());
        dump('$request->all()：', $request->all());

        $request->flash();
        return redirect('order')->withInput();
    }

    public function data(Request $request): void
    {
        dump($request->cookie());
    }

    public function file(Request $request): void
    {
        $file1 = $request->file('file');
        $file2 = $request->file;
        dump($file1, $file2, $request->hasFile('file'));
        $request
            ->file('file')
            ->isValid()
        ;
        $request
            ->file('file')
            ->path()
        ;
        $request
            ->file('file')
            ->extension()
        ;
    }

    public function response(Response $response): Response
    {
        $response->setContent([1, 2, 3]);
        $response->setStatusCode(200);
        $response->headers->set('Content-Type', 'application/json');
        $response->withHeaders(["X-Header" => 'x-header']);
        return $response;
    }

    public function model(Goods $goods): array|Collection
    {
        return $goods->get();
    }

    public function redirect(): RedirectResponse
    {
//        return redirect('/order');
        return back()->withInput();
    }

    public function download(): BinaryFileResponse
    {
        $filePath = public_path('/favicon.ico');
        return response()->download($filePath);
//        return response()->file('robots.txt');
    }

    public function order(): ViewContracts
    {
        return view('order')
            ->with('order', 'dd')
            ->with(['name' => 'ZhangSan', 'age' => 18])
            ->with('sex', 'Man')
        ;
//        return view('order', ['order' => 'order-order']);
//        return View::make('order', ['order' => 'oo']);
//        return View::first(['admin.order', 'order'], ['order' => 'oo']);
//        if (View::exists('admin.order')) {
//            return View::make('admin.order');
//        } elseif (View::exists('order')) {
//            return View::make('order', ['order' => 'rr']);
//        }
    }

    public function view(): ViewContracts
    {
        $data = [
            'name' => 'ZhangSan',
            'age' => 18,
            'html' => '<b> Bold </b>',
            'array' => [
                'name' => 'ZhangSan',
                'age' => 18,
                'html' => '<b> Bold </b>',
            ],
        ];
        return View::make('order.view', $data);
    }

    public function urls(Request $request): ViewContracts
    {
        dump(action([orderController::class, 'view']));
        dump($request->hasValidSignature(URL::signedRoute('order.view')));
        dump($request->hasValidSignature());
        dump(URL::temporarySignedRoute('order.view', expiration: now()->addMinute(30), parameters: ['foo' => 'bar']));
        dump(URL::signedRoute('order.view', ['user' => 1], absolute: false));
        dump(URL::signedRoute('order.view', ['user' => 1]));
        dump(route('order.view'));
        dump(url('/order'));
        dump(url()->full());
        dump(url()->current());
        dump(url()->previous());
        dump(URL::previous());
        dump(URL::current());

        return view('welcome');
    }
}
